Package gwtappcontainer.server.apis.admin

Source Code of gwtappcontainer.server.apis.admin.GateKeeperTest

package gwtappcontainer.server.apis.admin;

import static org.junit.Assert.assertTrue;

import java.util.UUID;

import gwtappcontainer.server.apis.admin.AdminAPI;
import gwtappcontainer.server.apis.admin.GateKeeper;
import gwtappcontainer.server.apis.admin.Roles.Role;
import gwtappcontainer.server.apps.APIException;
import gwtappcontainer.shared.apis.APIResponse;
import gwtappcontainer.shared.apis.APIResponse.Status;
import gwtappcontainer.testhelpers.APITestHelper;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.api.users.User;

public class GateKeeperTest {
 
  private final APITestHelper helper = new APITestHelper();
 
  @Before
  public void setUp() {
    helper.setUp();
  }
 
  @After
  public void tearDown() {
    helper.tearDown();
  }
 
  @Test
  public void ensureRoleReturnsTrueForExistingRole() {
       
    GateKeeper gateKeeper = new GateKeeper();
    User user = helper.loginAsDeveloper();
    assertTrue(gateKeeper.ensureRole(user, Role.DEVELOPER, Role.PORTAL_ADMIN));
   
    assertTrue(gateKeeper.ensureRole(user, Role.DEVELOPER.toString(),
        Role.PORTAL_READONLY.toString()));   
  }
 
  @Test
  public void ensureRoleThrowsExceptionForNonExistingRole() {
   
    GateKeeper gateKeeper = new GateKeeper();
    User user = helper.loginAsDeveloper();
   
    try {
      gateKeeper.ensureRole(user, Role.PORTAL_ADMIN);
    } catch (APIException e) {
      assertTrue(e.statusCode == Status.ERROR_INSUFFICIENT_PERMISSION);
      return;
    }
     
    //fail if it comes here
    assertTrue(false);
  }
 
  @Test
  public void ensureRoleThrowsExceptionIfNotLoggedIn() {
    GateKeeper gateKeeper = new GateKeeper();
       
    try {
      gateKeeper.ensureRole(null, Role.PORTAL_READONLY.toString());
    } catch (APIException e) {
      assertTrue(e.statusCode == Status.ERROR_LOGIN_REQUIRED);
      return;
    }
     
    //fail if it comes here
    assertTrue(false);
  }
 
  @Test
  public void ensureValidUserReturnsTrueForValidUser() {
       
    AdminAPI adminApi = new AdminAPI();
    String email = "test_" + UUID.randomUUID() + "@example.com";
    APIResponse resp = adminApi.addUser(email, helper.loginAsPortalAdmin());
    assertTrue(resp.statusCode == Status.SUCCESS);
   
    User user = helper.loginAs(email);
    GateKeeper gateKeeper = new GateKeeper();
    assertTrue(gateKeeper.ensureValidUser(user));     
  }
 
  @Test
  public void ensureValidUserThrowsExceptionForInvalidUser() {           
    String email = "test_" + UUID.randomUUID() + "@example.com"
    User user = helper.loginAs(email);
    GateKeeper gateKeeper = new GateKeeper();
   
    try {
      gateKeeper.ensureValidUser(user);
    } catch (APIException e) {
      assertTrue(e.statusCode == Status.ERROR_INVALID_USER);
      return;
    }
   
    //fail if it comes here
    assertTrue(false);
  }
 
  @Test
  public void ensureValidUserWithoutLoggingThrowsAuthenticationException() {           
   
    GateKeeper gateKeeper = new GateKeeper();
   
    try {
      gateKeeper.ensureValidUser(null);
    } catch (APIException e) {
      assertTrue(e.statusCode == Status.ERROR_LOGIN_REQUIRED);
      return;
    }
   
    //fail if it comes here
    assertTrue(false);
  }
}
TOP

Related Classes of gwtappcontainer.server.apis.admin.GateKeeperTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.